package com.cictec.middleware.gps.engine.send.apollo;

import java.util.Map;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cic.camel.components.stomp.StompComponent;
import com.cictec.middleware.commons.utils.log.MultiLog;
import com.cictec.middleware.commons.utils.log.MultiLogFileNameUtils;
import com.cictec.middleware.commons.utils.log.MultiLogUtils;
import com.cictec.middleware.gps.engine.send.pojo.BusPositionConvert;
import com.cictec.middleware.gps.engine.send.pojo.BusPositionMessage;


/**
 * 直线图 数据处理类
 * 数据转发处理器。 从通讯中间件接收实时车辆位置信息转发到STOMP服务器。
 * @author Administrator
 *
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Component
public class StraightLineProcessor implements Processor {

	
	private static Logger logger = LoggerFactory.getLogger(StraightLineProcessor.class);


	@Override
	public void process(Exchange exchange) throws Exception {

		/*
		byte[] byteStr = (byte[]) exchange.getIn().getBody();
		String body = new String(byteStr, "UTF-8");
		Map<String, Object> body = JSON.parseObject(body, Map.class);

		Map body = (Map<String, Object>) exchange.getIn().getBody();
		 */

		String jsonBody = (String) exchange.getIn().getBody();
		Map<String, Object> body = JSON.parseObject(jsonBody, Map.class);
		
		BusPositionMessage baseMessage = BusPositionConvert.explanMsg(body);
		
		StringBuffer urlBuf = new StringBuffer("/topic/").append("pos.").append(baseMessage.getLineUuid()).append(".").append(baseMessage.getBusUuid());
		exchange.getIn().setHeader(StompComponent.STOMP_DESTINATION, urlBuf.toString());

		String json = JSON.toJSONString(baseMessage, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteMapNullValue);
		exchange.getIn().setBody(json);

		// 日志记录
		String logPath = MultiLogFileNameUtils.getApolloLogName(baseMessage.getDevCode() + "-" +baseMessage.getBusPlate());
		MultiLog log = MultiLogUtils.getMultiLog(logPath);
		log.debug(json);
		
		logger.debug("转发至apollo topic: {} \n message:{}", urlBuf, json);
	}

	
}
